// src/types/customer.ts

/** 客户基础信息 */
export interface Customer {
  id: number
  code: string
  name: string
  area?: string
  address?: string
  createdBy: number
  createDate: Date | string
  updateId?: string
  updateDate: Date | string
  money: number
  phoneNum?: string
  isDelete: 0 | 1
}

/** 客户表单类型（用于新增/编辑） */
export interface CustomerForm extends Omit<Customer, 'id' | 'createDate' | 'updateDate'> {
  id?: number
  phoneNum?: string
}

/** 客户查询参数 */
export interface CustomerQuery {
  code?: string
  name?: string
  area?: string
  createDateRange?: [Date | string, Date | string]
  page?: number
  pageSize?: number
  sortField?: keyof Customer
  sortOrder?: 'asc' | 'desc'
}

/** 客户表单验证规则 */
export const customerRules = {
  code: [
    { required: true, message: '客户编码不能为空', trigger: 'blur' },
    { max: 255, message: '编码长度不能超过255字符', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '客户名称不能为空', trigger: 'blur' },
    { max: 255, message: '名称长度不能超过255字符', trigger: 'blur' }
  ],
  phoneNum: [
    {
      pattern: /^1[3-9]\d{9}$/,
      message: '手机号格式不正确',
      trigger: 'blur'
    }
  ],
  money: [
    {
      type: 'number',
      min: 0,
      message: '金额不能小于0',
      trigger: 'blur'
    }
  ]
}

/** 初始化空客户表单 */
export const initCustomerForm = (): CustomerForm => ({
  code: '',
  name: '',
  area: undefined,
  address: undefined,
  createdBy: 0,
  updateId: undefined,
  money: 0,
  phoneNum: undefined,
  isDelete: 0
})

/** 金额格式化（保留2位小数） */
export const formatMoney = (value: number) => {
  return value.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

/** 状态转换 */
export const customerStatus = {
  0: { text: '正常', color: 'green' },
  1: { text: '已删除', color: 'red' }
} as const